﻿using IcicleFramework.Components.Behaviors;
using IcicleFramework.Components.Collision;
using Microsoft.Xna.Framework;
using TestBed.Actions;

namespace TestBed.Components.Behaviors
{
    public class PlayerCollisionBehavior : BaseBehavior
    {
        private bool colliding;
        
        public override void Initialize()
        {
            //Subscribe to our Collision Components OnCollision event
            var collisionComponent = ParentGameObject.GetComponent<ICollisionComponent>();
            collisionComponent.OnCollision += CollisionHandler;

            base.Initialize();
        }

        private void CollisionHandler(ICollisionComponent source, ICollisionComponent collided)
        {
                colliding = true;
                Parent.FireAction(new ColorChangeAction() { Target = ParentGameObject, Color = Color.Red });
                //Parent.FireSingleAction(ParentGameObject, ActionName, actionData);
            
        }

        public override void Update(GameTime gameTime)
        {
            if (!colliding)
            {
                Parent.FireAction(new ColorChangeAction() { Target = ParentGameObject, Color = Color.Purple });
            }

            colliding = false;

            base.Update(gameTime);
        }

        public override IBehavior DeepClone()
        {
            return new PlayerCollisionBehavior();
        }
    }
}
